class LastJiFenRankView extends BaseView{

    constructor(){
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.initView,this);
    }

    private bgMaskShap:egret.Shape;
    private gr_btn:egret.Bitmap;
    private bh_btn:egret.Bitmap;
    private bg_jf:egret.Bitmap;
    private my_rank:egret.DisplayObjectContainer;
    private tableView:TableView.TableView;
    private gr_text:egret.TextField;
    private bh_text:egret.TextField;
    private contentviewbg:egret.Bitmap;
    private now_tab:number = 0;
    private title:egret.TextField;
    private close_btn:egret.Bitmap;
    private gr_Container:egret.DisplayObjectContainer;
    private bh_Container:egret.DisplayObjectContainer;
    private sq_btn:egret.Bitmap;
    private contentview:egret.DisplayObjectContainer;
    private emptyLabel_text:egret.TextField;
    private emptyLabel_img:egret.Bitmap;
    private todayLabel:egret.TextField;
    private award_list_view:egret.DisplayObjectContainer;
    private todayShape:egret.Shape;
    private _timeLabel:egret.TextField;
    private bgMaskAward:egret.Shape;
    private props_arr:any ;


    private queryGrRank():void{
        this.now_tab = 1;
        var _that = this;
        CurlUtil.curl(config.api_url + '/bxg/rank/personal/last',{}).then(function(data:any){
            if(_that.now_tab == 2){
                return;
            }
            if(data.status == "success"){
               _that.createGrJiFenView(data.data);
               // 更新时间显示
               _that.updateDate(data.data['next_time'], data.data['date']);
            }else{
                // Toast.makeText({ text: '调用接口失败！{code:'+data.code+'}' }).show();
                 _that.showEmpty();
            }
        }).catch(function(){
            // Toast.makeText({ text: '调用接口失败！' }).show();
            _that.showEmpty();
        });
    }

    private queryBhRank():void{
        this.now_tab = 2;
        var _that = this;
        CurlUtil.curl(config.api_url + '/bxg/rank/gang/last',{}).then(function(data:any){
            if(_that.now_tab == 1){
                return;
            }
            if(data.status == "success"){
               _that.createBhJiFenView(data.data);
               // 更新时间显示
               _that.updateDate(data.data['next_time'], data.data['date']);
            }else{
                // Toast.makeText({ text: '调用接口失败！{code:'+data.code+'}' }).show();
                 _that.showEmpty();
            }
        }).catch(function(){
            // Toast.makeText({ text: '调用接口失败！' }).show();
            _that.showEmpty();
        });
    }

    private showEmpty():void{
        this.tableView.dataSource = [];
        if(this.contentview.contains(this.my_rank)){
            this.contentview.removeChild(this.my_rank);
        }
        this.emptyLabel_text.visible = true;
        this.emptyLabel_img.visible = true;
    }


    private initView():void{

        this.props_arr = {
            "gr":[],
            "bh":[]
        };

        //背景
        if(!this.contains(this.bgMaskShap)){
            var bgMaskShap = new egret.Shape();
            bgMaskShap.graphics.beginFill(0x000000, 0.6);
            bgMaskShap.graphics.drawRect(0, 0, this.width, this.height);
            bgMaskShap.graphics.endFill();
            bgMaskShap.touchEnabled = true;
            this.bgMaskShap = bgMaskShap;
            this.addChild(bgMaskShap);
        }

        if(!this.contains(this.contentview)){
            this.contentview = new egret.DisplayObjectContainer;
            this.addChild(this.contentview);
            this.contentview.width = this.width;
            this.contentview.height = this.height;
            this.contentview.x = (this.contentview.width)/2;
            this.contentview.y = (this.contentview.height)/2;
            this.contentview.anchorOffsetX = this.contentview.width/2;
            this.contentview.anchorOffsetY = this.contentview.height/2;
        }
        

        //背景图
        if(!this.contentview.contains(this.contentviewbg)){
            var contentviewbg = utils.createBitmapByName('friend');
            contentviewbg.scale9Grid = new egret.Rectangle(250, 160, 100, 50);
            contentviewbg.height = this.height*0.9;
            contentviewbg.width = this.width*0.95;
            contentviewbg.anchorOffsetX = contentviewbg.width/2;
            contentviewbg.anchorOffsetY = contentviewbg.height/2;
            contentviewbg.x = (this.width )/2;
            contentviewbg.y = (this.height )/2;
            this.contentviewbg = contentviewbg;
            this.contentview.addChild(contentviewbg);
        }

        //tabbar背景
        if(!this.contentview.contains(this.bg_jf)){
            var bg_jf = utils.createBitmapByName("bg_jf");
            bg_jf.width = contentviewbg.width*0.85;
            bg_jf.height = this.height*0.58;
            bg_jf.y = 300;
            bg_jf.x = (this.width - bg_jf.width)/2;
            this.contentview.addChild(bg_jf);
            this.bg_jf = bg_jf;
        }

        //积分榜
        if(!this.contentview.contains(this.title)){

            var titleLabel = new BaseTextField();
            titleLabel.text = "上期奖励分配";
            titleLabel.width = this.contentview.width ;
            titleLabel.x = (this.width - titleLabel.width)/2;
            titleLabel.y = 90;
            titleLabel.textAlign = egret.HorizontalAlign.CENTER;
            titleLabel.stroke = 4;
            titleLabel.strokeColor = 0xBA7114;
            titleLabel.textColor = 0xF2E019;
            titleLabel.bold = true;
            titleLabel.size = 38;
            this.title = titleLabel;
            this.contentview.addChild(this.title);

            var todayLabel = new BaseTextField();
            todayLabel.textColor = 0xFFF894;
            todayLabel.text = "";
            todayLabel.width = 188;
            todayLabel.height = 35;
            todayLabel.size = 26;
            todayLabel.textAlign = egret.HorizontalAlign.CENTER;
            todayLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            todayLabel.x = (this.contentview.width - todayLabel.width) / 2.0;
            todayLabel.y = utils.getMaxY(this.title) + 10;
            this.todayLabel = todayLabel;

            var todayShape = new egret.Shape();
            todayShape.x = this.todayLabel.x;
            todayShape.y  = this.todayLabel.y;
            todayShape.width  = this.todayLabel.width;
            todayShape.height = this.todayLabel.height;
            todayShape.graphics.beginFill(0x7B490D);
            todayShape.graphics.drawRoundRect(0, 0, todayShape.width, todayShape.height, 20);
            todayShape.graphics.endFill();
            this.todayShape = todayShape;
            this.contentview.addChild(this.todayShape);

            
            this.contentview.addChild(this.todayLabel);
        }

        //关闭按钮
        if(!this.contentview.contains(this.close_btn)){
            var close_btn = utils.createBitmapByName("x",{offsetX:0.5, offsetY:0.5});
            close_btn.x = 682;
            close_btn.y = 136;
            close_btn.touchEnabled = true;
            close_btn.touchAnimation = true;
            this.close_btn = close_btn;
            this.contentview.addChild(close_btn);
            close_btn.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
                Main.removeLastJFView();
            },this);
        }


        //个人排行榜
        if(!this.contentview.contains(this.gr_Container)){
            var gr_Container = new egret.DisplayObjectContainer;
            this.contentview.addChild(gr_Container);
            this.gr_Container = gr_Container;

            var gr_btn = utils.createBitmapByName("tab1");
            gr_btn.x = 70;
            gr_btn.y = 210;
            gr_btn.touchEnabled = true;
            gr_Container.addChild(gr_btn);
            this.gr_btn = gr_btn;

            var gr_text = utils.createTextField();
            gr_text.x = gr_btn.x;
            gr_text.y = 210;
            gr_text.width = gr_btn.width;
            gr_text.height = (gr_btn.width*66)/190;
            gr_text.textAlign = "center";
            gr_text.verticalAlign = egret.VerticalAlign.MIDDLE;
            gr_text.text = "个人积分榜";
            gr_text.size = 30;
            gr_text.textColor = 0xFFEA70;
            gr_text.stroke = 2;
            gr_text.bold = true;
            gr_text.strokeColor = 0x6A410D;
            this.gr_text = gr_text;
            gr_Container.addChild(gr_text);
            gr_Container.touchEnabled = true;
            gr_Container.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
                this.gr_btn.texture = RES.getRes("tab1");
                this.bh_btn.texture = RES.getRes("tab2");
                this.gr_text.textColor = 0xFFEA70;
                this.bh_text.textColor = 0xEBC275;
                if(this.now_tab == 1){
                    return;
                }
                this.queryGrRank();
            },this);
        }

        //帮会排行榜
        if(!this.contentview.contains(this.bh_Container)){
            var bh_Container = new egret.DisplayObjectContainer;
            this.contentview.addChild(bh_Container);
            this.bh_Container = bh_Container;

            var bh_btn = utils.createBitmapByName("tab2");
            bh_btn.x = gr_btn.x + gr_btn.width + 30;
            bh_btn.y = 210;
            bh_btn.touchEnabled = true;
            bh_Container.addChild(bh_btn);
            this.bh_btn = bh_btn;

            var bh_text = utils.createTextField();
            bh_text.x = bh_btn.x;
            bh_text.width = bh_btn.width;
            bh_text.y = 210;
            bh_text.height = (bh_btn.width*66)/190;
            bh_text.textAlign = "center";
            bh_text.verticalAlign = egret.VerticalAlign.MIDDLE;
            bh_text.text = "帮会积分榜";
            bh_text.size = 30;
            bh_text.textColor = 0xEBC275;
            bh_text.stroke = 2;
            bh_text.bold = true;
            bh_text.strokeColor = 0x6A410D;
            bh_Container.addChild(bh_text);
            this.bh_text = bh_text;
            bh_Container.touchEnabled = true;
            bh_Container.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
                this.bh_btn.texture = RES.getRes("tab1");
                this.gr_btn.texture = RES.getRes("tab2");
                this.bh_text.textColor = 0xFFEA70;
                this.gr_text.textColor = 0xEBC275;
                if(this.now_tab == 2){
                    return;
                }
                this.queryBhRank();
            },this);
        }

        //列表
        if(!this.contentview.contains(this.tableView)){
            var tableView = new TableView.TableView();
            tableView.height = this.bg_jf.height - 15;
            tableView.scrollPolicyH = "false";   
            // tableView.scrollPolicyV = "false";  
            tableView.width = this.bg_jf.width;
            tableView.anchorOffsetX=tableView.width/2;
            tableView.x = (this.width )/2;
            tableView.y = this.bg_jf.y + 6;
            this.tableView = tableView;
            this.contentview.addChild(this.tableView);
        }


        if(!this.contentview.contains(this.emptyLabel_img)){

            this.emptyLabel_img = utils.createBitmapByName("dj_icon");
            this.emptyLabel_img.x = (this.width - this.emptyLabel_img.width)/2;
            this.emptyLabel_img.y = (this.height - this.emptyLabel_img.height)/2;
            this.contentview.addChild(this.emptyLabel_img);

            this.emptyLabel_img.visible = false;

        }

        if(!this.contentview.contains(this.emptyLabel_text)){
            this.emptyLabel_text = new BaseTextField();
            this.emptyLabel_text.text = "暂无数据!";
            this.emptyLabel_text.width = this.width;
            this.emptyLabel_text.y = this.contentview.y + this.emptyLabel_img.height/2 + 10;
            this.emptyLabel_text.textColor = 0xc19b61;
            this.emptyLabel_text.visible = false;
            this.emptyLabel_text.textAlign = "center";
            this.contentview.addChild(this.emptyLabel_text);

        }


        if(!this.contentview.contains(this._timeLabel)){
            var timeLabel = new BaseTextField();
            timeLabel.height = 110;
            timeLabel.textAlign = egret.HorizontalAlign.CENTER;
            timeLabel.textColor = 0x7C552C;
            timeLabel.lineSpacing  = 10;
            timeLabel.width = (this.contentview.width );
            timeLabel.x = (this.width - timeLabel.width)/2;
            timeLabel.y = this.tableView.y + this.tableView.height + 10;
            timeLabel.text = "下次活动开启时间\n";
            timeLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            timeLabel.size = 26;
            timeLabel.bold = true;
            this._timeLabel = timeLabel;
            this.contentview.addChild(this._timeLabel);
        }
        
        if(this.now_tab == 2){
            this.queryBhRank();
        }else{
            this.queryGrRank();
        }

        this.addAnimation();

    }


    // 添加动画
    private addAnimation(){
        this.contentview.scaleX = this.contentview.scaleY = 0;
        egret.Tween.removeTweens(this.contentview);
        egret.Tween.get(this.contentview).to({scaleX:1, scaleY:1}, 500, egret.Ease.backOut);
    }

    /**
     * 创建个人排行榜
     */
    public createGrJiFenView(data:any):void{

        this.emptyLabel_text.visible = false;
        this.emptyLabel_img.visible = false;

        var dataSource = data.list;
        this.tableView.dataSource = dataSource;
        this.tableView.itemRenderer = JiFenCell;
        JiFenCell.CELLWIDTH = this.tableView.width;   
        JiFenCell.jfView = this;

        if(dataSource.length <= 0){
            
            this.emptyLabel_text.visible = true;
            this.emptyLabel_img.visible = true;
        }else{
            for(var i =0;i<dataSource.length;i++){
               this.props_arr.gr.push(dataSource[i].props);
            }
        }

        if(this.contentview.contains(this.my_rank)){
            this.contentview.removeChild(this.my_rank);
        }

    }


    /**
     * 创建帮会排行榜
     */
    public createBhJiFenView(data:any):void{

        this.emptyLabel_text.visible = false;
        this.emptyLabel_img.visible = false;

        var dataSource = data.list;
        this.tableView.dataSource = dataSource;
        this.tableView.itemRenderer = BhJiFenCell;
        BhJiFenCell.CELLWIDTH = this.tableView.width;
        BhJiFenCell.jfView = this;

        if(dataSource.length <= 0){
           this.emptyLabel_text.visible = true;
            this.emptyLabel_img.visible = true;
        } else{
            for(var i =0;i<dataSource.length;i++){
               this.props_arr.bh.push(dataSource[i].props);
            }
        }

        //帮会排名
        if(this.contentview.contains(this.my_rank)){
            this.contentview.removeChild(this.my_rank);
        }

    }


    public showAwardList(index:number,x:number,evt:egret.TouchEvent,type:string){
        
        if(this.contentview.contains(this.bgMaskAward)){
            this.contentview.removeChild(this.bgMaskAward);
        }

        this.bgMaskAward = new egret.Shape();
        this.bgMaskAward.graphics.beginFill(0x000000, 0);
        this.bgMaskAward.graphics.drawRect(0, 0, this.width, this.height);
        this.bgMaskAward.graphics.endFill();
        this.bgMaskAward.touchEnabled = true;
        this.contentview.addChild(this.bgMaskAward);

        this.bgMaskAward.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
               this.bgMaskAward.visible = false;
               this.award_list_view.visible = false;
        },this);

        if(this.contentview.contains(this.award_list_view)){
            this.contentview.removeChild(this.award_list_view);
        }
        this.award_list_view = new egret.DisplayObjectContainer;
        var bg = utils.createBitmapByName("award_bg");
        bg.width = this.bg_jf.width;
        
        this.award_list_view.addChild(bg);
        this.contentview.addChild(this.award_list_view);

        // this.award_list_view.y = y + this.tableView.y;
        this.award_list_view.y = evt.stageY;
        this.award_list_view.x = x - 440;
        this.award_list_view.height = bg.height;
        
        let data = this.props_arr[type][index-1];

        bg.height = 130 * (Math.floor(data.length/7) + 1) + 20;
        var width = (bg.width)/6;
        var height = 130;
        for(let i=1;i<=data.length;i++){
            var view = new egret.DisplayObjectContainer;
            view.width = width;
            view.height = height;
            view.anchorOffsetX = view.width/2;
            view.anchorOffsetY = view.height/2;
            if(i%6 == 0){
                view.x = width*5 + (width/2);
                view.y = 130 * (Math.floor(i/6)-1) + view.height/2;
            }else{
                view.x = width*(i%6 - 1) + (width/2);
                view.y = 130 * (Math.floor(i/6)) + view.height/2;
            }
            
            
            var  img:eui.Image = new eui.Image();
            let item = data[i-1];
            img.width = width * 0.75;
            img.height = width * 0.75;
            img.anchorOffsetX = img.width/2;
            img.anchorOffsetY = img.height/2;
            img.y = view.height/2;
            img.x = view.width/2;
            img.source = item.prop_img;
            view.addChild(img);

            var text = utils.createTextField();
            text.size = 16;
            text.textColor = 0x6A410D;
            text.bold = true;
            // text.x = (view.width - text.width)/2;
            text.y = img.y + img.height/2 + 5;
            text.width = view.width;
            if(item.count > 1){
              text.text = item.prop_name + " x"+item.count;
            }else{
              text.text = item.prop_name ;
            }
            view.addChild(text);
            this.award_list_view.addChild(view);
            view.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
                var data_tool = {};
                if(item.type == 1){
                    data_tool = {
                        "img":item.prop_img,
                        "name":item.prop_name,
                        "desc":item.prop_desc,
                        "type":item.type
                    }
                }else{
                    data_tool = {
                        "img":item.prop_img,
                        "name":item.prop_name,
                        "desc":item.prop_desc,
                        "type":item.type,
                        "parent_img":item.parent_img,
                        "parent_name":item.parent_name,
                        "parent_desc":item.parent_desc,
                    }
                }
                 Main.showTooInfoView(data_tool);
            },view);
        }
    }

    // 更新时间
    private updateDate(next:string, date:string){
        if(next){
            this._timeLabel.text = "下次活动开启时间\n" +next;
        }else{
            this._timeLabel.text = '';
        }
        if(date){
            this.todayLabel.text = date;
            this.todayShape.visible = true;
            this.title.y = 90;
        }else{
            this.todayLabel.text = '';
            this.todayShape.visible = false;
            this.title.y = 100;
        }
    }
}
